package com.dp.inspect;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dp.io.JSONUtil;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUrlClient {



    //get远程信息请求
    public static List<Map<String, Object>> get(String urlPath, String token, String com_coalcode, String date){
        String url = urlPath + "?token=" +token + "&companyCode=" + com_coalcode + "&date=" + date;
        System.out.println();
        System.out.println(url);
        List<Map<String, Object>> map = new ArrayList<>();
        try{
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-Type", "application/json;charset=utf8");
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String s = EntityUtils.toString(responseEntity);
                    System.out.println("===响应内容为:" +s);
                    JSONObject jsonObject=JSONObject.parseObject(s);
                    map = JSONUtil.JSONArrayTolist((JSONArray) jsonObject.get("data"));
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }
        return map;
    }

}
